package com.fansaikj.erp.util;

import cn.hutool.db.Db;
import xyz.erupt.core.constant.MenuTypeEnum;
import xyz.erupt.generator.model.GeneratorClass;
import java.sql.SQLException;
import java.util.List;

public class GenUtils {

    /**
     * 业务表名，支持模糊匹配
     */
    private static final String tableName = "erp_";

    public static List<GeneratorClass> getAllTables() throws SQLException {
        List<GeneratorClass> classList = Db.use().query("select table_name , table_comment as name, create_time, update_time " +
                "from information_schema.tables where table_schema = (select database()) and table_name like 'erp_%'", GeneratorClass.class);
        classList.forEach(item -> {
            String tableName = item.getTableName();
            String[] nameSplit = tableName.split("_");
            StringBuffer sb = new StringBuffer();
            for (String str : nameSplit) {
                sb.append(str.substring(0, 1).toUpperCase() + str.substring(1));
            }
            item.setClassName(sb.toString());
        });
        return classList;
    }

    public static void main(String[] args) throws SQLException {
        List<GeneratorClass> classList = getAllTables();
        System.out.println("classList========" + classList.size());
        for (GeneratorClass generatorClass : classList) {
            System.out.println("-- 菜单 SQL");
            String listSql = "INSERT INTO `e_upms_menu` (`create_by`, `create_time`, `update_by`, `update_time`, `code`, " +
                    "`icon`, `name`, `param`, `sort`, `status`, `type`, `value`, `parent_menu_id`) VALUES (NULL, now(), " +
                    "NULL, NULL, '%s', '', '%s', NULL, 100, 1, '%s', '%s', %s);\n";
            String parsedListSql = String.format(listSql, generatorClass.getClassName(), generatorClass.getName(), MenuTypeEnum.TABLE.getCode(), generatorClass.getClassName(), null);
            System.out.println(parsedListSql);
            System.out.println("-- 菜单父ID");
            System.out.println("SELECT @parentId := LAST_INSERT_ID();");
            System.out.println("-- 按钮 SQL");
            String parsedAddSql = String.format(listSql, generatorClass.getClassName() + "@ADD", "新增", MenuTypeEnum.BUTTON.getCode(), generatorClass.getClassName() + "@ADD", "@parentId");
            System.out.println(parsedAddSql);
            String parsedEditSql = String.format(listSql, generatorClass.getClassName() + "@EDIT", "修改", MenuTypeEnum.BUTTON.getCode(), generatorClass.getClassName() + "@EDIT", "@parentId");
            System.out.println(parsedEditSql);
            String parsedDelSql = String.format(listSql, generatorClass.getClassName() + "@DELETE", "删除", MenuTypeEnum.BUTTON.getCode(), generatorClass.getClassName() + "@DELETE", "@parentId");
            System.out.println(parsedDelSql);
            String parsedExpSql = String.format(listSql, generatorClass.getClassName() + "@EXPORT", "导出", MenuTypeEnum.BUTTON.getCode(), generatorClass.getClassName() + "@EXPORT", "@parentId");
            System.out.println(parsedExpSql);
            String parsedImpSql = String.format(listSql, generatorClass.getClassName() + "@IMPORTABLE", "导入", MenuTypeEnum.BUTTON.getCode(), generatorClass.getClassName() + "@IMPORTABLE", "@parentId");
            System.out.println(parsedImpSql);
            String parsedViewSql = String.format(listSql, generatorClass.getClassName() + "@VIEW_DETAIL", "详情", MenuTypeEnum.BUTTON.getCode(), generatorClass.getClassName() + "@VIEW_DETAIL", "@parentId");
            System.out.println(parsedViewSql);
        }
    }

}
